package num1114;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author : DuJiabao
 * @Project : sword-finger-offer
 * @Time : 2021/4/17 10:10
 * @Desc :
 */
class Foo {
  volatile boolean firstFinish = false;
  boolean secondFinish = false;
  ReentrantLock lock = new ReentrantLock();

  public Foo() {
  }

  public void first(Runnable printFirst) throws InterruptedException {
    lock.lock();
    try {
      printFirst.run();
    } finally {
      lock.unlock();
      firstFinish = true;
    }
  }

  public void second(Runnable printSecond) throws InterruptedException {
    while (!firstFinish) {
    }
    lock.lock();
    try {
      printSecond.run();
    } finally {
      lock.unlock();
      secondFinish = true;
    }
  }

  public void third(Runnable printThird) throws InterruptedException {
    while (!secondFinish) {
    }
    lock.lock();
    try {
      printThird.run();
    } finally {
      lock.unlock();
    }
  }
}
